package com.hht.baselibs.layoutManager

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import com.hht.baselibs.R


/**
 * Author:chen'hu
 * Time:2023/9/13 15:22
 * Description:This is PageIndicatorView
 */
class PageIndicatorView(context: Context, attrs: AttributeSet) :
    IndicatorView(context, attrs, 0), RecycleViewLayoutManager.PageListener {
    private var dotSize = 5 // 指示器的大小（dp）
    private var margins = 5 // 指示器间距（dp）
    private var indicatorViews = ArrayList<View>()
    private var mSelectResId: Int = R.drawable.shape_dog_select
    private var mNormalResId: Int = R.drawable.shape_dog_normal
    private var onPageChangeListener: OnPageChangeListener? = null
    private var pageSize: Int = 1

    fun setOnPageChangeListener(listener: OnPageChangeListener?) {
        this.onPageChangeListener = listener
    }

    init {
        gravity = Gravity.CENTER
        orientation = HORIZONTAL

        if (width > 0 || height > 0) {
            dotSize = width.coerceAtMost(height)
        }

        dotSize = dip2px(context, dotSize.toFloat())
        margins = dip2px(context, margins.toFloat())
    }

    //设置圆点大小,设置多大就是多大，不需要转换
    fun setIndicatorViewDotSize(size: Int) {
        if (size < 0) return else {
            dotSize = size
        }
    }

    /**
     * 初始化指示器，默认选中第一页
     *
     * @param count 指示器数量，即页数
     */
    private fun initIndicator(count: Int) {
        if (indicatorViews != null) {
            indicatorViews.clear()
            removeAllViews()
        }
        var view: View
        val params = LayoutParams(dotSize, dotSize)
        params.setMargins(margins, margins, margins, margins)
        for (i in 0 until count) {
            view = View(context)
            view.setBackgroundResource(mNormalResId)
            addView(view, params)
            indicatorViews.add(view)
        }
        if (indicatorViews.size > 0) {
            indicatorViews[0].setBackgroundResource(mSelectResId)
        }
        // 初始化必须加上，不然第一次不显示
        measure(width, height)
    }

    /**
     * 设置选中页
     *
     * @param selected 页下标，从0开始
     */
    fun setSelectedPage(selected: Int) {
        for (i in indicatorViews.indices) {
            if (i == selected) {
                indicatorViews[i].setBackgroundResource(mSelectResId)
            } else {
                indicatorViews[i].setBackgroundResource(mNormalResId)
            }
        }
    }

    override fun setLayoutManagerPageListener(recycleViewLayoutManager: RecycleViewLayoutManager?) {
        recycleViewLayoutManager?.setPageListener(this)
    }

    override fun onPageSizeChanged(pageSize: Int) {
        this.pageSize = pageSize
        initIndicator(pageSize)
        onPageChangeListener?.onPageSizeChanged(pageSize)
    }

    override fun onPageSelect(pageIndex: Int) {
        setSelectedPage(pageIndex)
        onPageChangeListener?.onPageSelect(pageIndex)
    }

    fun setSelectResId(resId: Int) {
        this.mSelectResId = resId
    }

    fun setNormalResId(resId: Int) {
        this.mNormalResId = resId
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     *
     * @param context
     * @param dpValue 要转换的dp值
     */
    private fun dip2px(context: Context, dpValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     *
     * @param context
     * @param pxValue 要转换的px值
     */
    private fun px2dip(context: Context, pxValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (pxValue / scale + 0.5f).toInt()
    }

    interface OnPageChangeListener {
        fun onPageSelect(pageIndex: Int)
        fun onPageSizeChanged(pageSize: Int)
    }
}